package com.xindaoapp.happypet.ui;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.view.ProgressWebView;

/**
 * 带进度条的WebView
 * 
 * @author Yangqing
 * 
 */
public class ProgressBarWebViewActivity extends BaseActivity {
    private ProgressWebView web;

    // private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_progressbar_webview;
    }

    @Override
    protected void initViews() {
        super.initViews();

        if (getIntent().hasExtra(Constants.KEY_TOP_BAR_TITLE)) {
            setTopBarTitle(getIntent().getStringExtra(Constants.KEY_TOP_BAR_TITLE));
        }

        // Here is all of our WebView Components
        web = (ProgressWebView) findViewById(R.id.webview);
        web.setBackgroundColor(Color.parseColor("#ffffff"));

        this.web.setWebViewClient(new webViewClient());

        web.getSettings().setJavaScriptEnabled(true);
        web.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition,
                    String mimetype, long contentLength) {
                if (url != null && url.startsWith("http://"))
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            }
        });

        web.loadUrl(getIntent().getStringExtra(Constants.KEY_VIDEO_URL));



        // // progressBar = (ProgressBar) findViewById(R.id.progressbar);
        // web.setWebChromeClient(new WebChromeClient() {
        // @Override
        // public void onProgressChanged(WebView view, int progress) {
        // // progressBar.setProgress(progress);
        // }
        // });
        WebSettings websettings = web.getSettings();
        // websettings.setBuiltInZoomControls(true);
        websettings.setUseWideViewPort(true);
        websettings.setJavaScriptEnabled(true);
        websettings.setAllowFileAccess(true);
        websettings.setDomStorageEnabled(true);
        websettings.setLoadWithOverviewMode(true);
        // websettings.setUserAgentString("Android");
        websettings.setAppCacheEnabled(true);
        websettings.setSavePassword(true);

        // // Load the URL you want to load
        // web.loadUrl(getIntent().getStringExtra(Constants.KEY_VIDEO_URL));
        // Also can be used like this
        // web.loadUrl("http://www.url.com"");
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        };
    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }

    @Override
    protected void initEvents() {
        super.initEvents();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (web.canGoBack()) {
                web.goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        web.removeAllViews();
        web.destroy();
    }

    public class webViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

    }


}
